package com.ticket.sass.admin.service.impl;


import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ticket.sass.admin.mapper.UserMapper;
import com.ticket.sass.admin.service.BaseService;
import com.ticket.sass.common.entity.tenant.UserEntity;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author ywb
 * @since 2024-03-06
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, UserEntity> implements BaseService<UserEntity> {
    public List<Long> getUserIdByCellphone(String cellphone) {
        if (StrUtil.isEmpty(cellphone)){
            return new ArrayList<>();
        }
        
        QueryWrapper<UserEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("cellphone", cellphone);
        List<UserEntity> userList = baseMapper.selectList(queryWrapper);
        List<Long> userIds = userList.stream().map(UserEntity::getId).collect(Collectors.toList());
        return userIds;
    }
    public Map<Long, String> getUserIdsWithCellphones(List<Long> userIds) {
        if (CollectionUtil.isEmpty(userIds)){
            return new HashMap<>();
        }
        
        QueryWrapper<UserEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("id", userIds);
        queryWrapper.select("id", "cellphone");
        
        List<UserEntity> userList = baseMapper.selectList(queryWrapper);
        
        Map<Long, String> userIdsWithCellphones = userList.stream()
            .collect(Collectors.toMap(UserEntity::getId, UserEntity::getCellphone));
        
        return userIdsWithCellphones;
    }
    
    public List<Object[]> getDailyUserCountsForPastWeek() {
        // 获取当前日期和一周前的日期
        LocalDate now = LocalDate.now();
        LocalDate oneWeekAgo = now.minusDays(7);
        
        // 调用mapper获取数据
        List<Map<String, Object>> userCounts = this.baseMapper.getUserCountByDay(oneWeekAgo, now.plusDays(1));
        
        // 转换数据格式
        Map<LocalDate, Integer> userCountMap = userCounts.stream()
            .collect(Collectors.toMap(
                entry -> ((java.sql.Date) entry.get("day")).toLocalDate(), // 转换日期
                entry -> ((Long) entry.get("user_count")).intValue() // 用户计数
            ));
        
        // 填充每一天的日期，如果没有计数则默认为0
        List<Object[]> dailyCounts = new ArrayList<>();
        for (LocalDate day = oneWeekAgo; day.isBefore(now.plusDays(1)); day = day.plusDays(1)) {
            dailyCounts.add(new Object[]{day, userCountMap.getOrDefault(day, 0)});
        }
        
        return dailyCounts;
    }
    
    // 将java.sql.Date转换为java.time.LocalDate
    private LocalDate convertToDate(Object date) {
        if (date instanceof java.sql.Date) {
            java.sql.Date sqlDate = (java.sql.Date) date;
            return sqlDate.toLocalDate();
        } else {
            throw new IllegalArgumentException("Expected a java.sql.Date object, but got " + date.getClass().getName());
        }
    }
    
    // 辅助方法，根据日期返回对应的中文星期
    private String getDayOfWeek(Integer day) {
        String[] daysOfWeek = {"日", "一", "二", "三", "四", "五", "六"};
        return daysOfWeek[day - 1];
    }
}
